import os

from docx import Document
from docxtpl import DocxTemplate
from docxtpl import DocxTemplate,InlineImage,RichText
from docx.shared import Mm
class LibreOfficeFile():
    dirParam = "/Volumes/软件/FHPY/pythonDocToPdf"
    def docToPdf(self):
        tpl = DocxTemplate('../DOC/list.docx')
        context = {
            'date': '2020年08月08日',
            'depart': '销售部',
            'username': '宋公明',
            "img":InlineImage(tpl,'logo1.jpg', width=Mm(30), height=Mm(15)),
            'fee_list': [
                {
                    "uid": "2a2f409d-3c20-44ec-b2ae-8364da6b8477",
                    "startDate": "2020-08-04",
                    "startPoint": "1",
                    "arriveDate": "2020-08-04",
                    "arrivePoint": "2",
                    "transport": "高铁",
                    "transportFee": "0.00",
                    "days": 0.0,
                    "missionAllowance": "0.00",
                    "hotelExpense": "0.00",
                    "urbanTransport": "0.00",
                    "otherExpense": "0.00",

                },
                {
                    "uid": "3f26ac5b-3f10-4917-971a-4609137a196e",
                    "startDate": "2020-08-01",
                    "startPoint": "常州",
                    "arriveDate": "2020-08-03",
                    "arrivePoint": "济南",
                    "transport": "高铁",
                    "transportFee": "300.00",
                    "days": 0.0,
                    "missionAllowance": "0.00",
                    "hotelExpense": "0.00",
                    "urbanTransport": "0.00",
                    "otherExpense": "0.00",
                },
                {
                    "uid": "910bb069-88a2-480c-a653-ae3bb8a7c84f",
                    "startDate": "2020-08-01",
                    "startPoint": "杭州",
                    "arriveDate": "2020-08-01",
                    "arrivePoint": "杭州",
                    "transport": "高铁",
                    "transportFee": "0.00",
                    "days": 0.0,
                    "missionAllowance": "0.00",
                    "hotelExpense": "0.00",
                    "urbanTransport": "0.00",
                    "otherExpense": "0.00",

                },
                {
                    "uid": "a8469b44-953a-409a-9c54-156b69931956",
                    "startDate": "2020-08-01",
                    "startPoint": "济南",
                    "arriveDate": "2020-08-01",
                    "arrivePoint": "济南",
                    "transport": "高铁",
                    "transportFee": "0.00",
                    "days": 0.0,
                    "missionAllowance": "0.00",
                    "hotelExpense": "0.00",
                    "urbanTransport": "0.00",
                    "otherExpense": "0.00",

                },
                {
                    "uid": "5ef4eb56-bc24-4701-8b7e-e2c4c0cd8afd",
                    "startDate": "2020-08-01",
                    "startPoint": "石家庄",
                    "arriveDate": "2020-08-01",
                    "arrivePoint": "石家庄",
                    "transport": "高铁",
                    "transportFee": "4.00",
                    "days": 0.0,
                    "missionAllowance": "0.00",
                    "hotelExpense": "0.00",
                    "urbanTransport": "0.00",
                    "otherExpense": "0.00",

                },
                {
                    "uid": "134416ff-23a4-45ac-8797-47dc49eeb399",
                    "startDate": "2020-08-01",
                    "startPoint": "333",
                    "arriveDate": "2020-08-01",
                    "arrivePoint": "4444",
                    "transport": "高铁",
                    "transportFee": "0.00",
                    "days": 0.0,
                    "missionAllowance": "0.00",
                    "hotelExpense": "0.00",
                    "urbanTransport": "0.00",
                    "otherExpense": "0.00",

                },
                {
                    "uid": "affeaccb-1f23-4a22-9816-6ed8739a5437",
                    "startDate": "2020-08-01",
                    "startPoint": "常州2",
                    "arriveDate": "2020-08-01",
                    "arrivePoint": "衡水2",
                    "transport": "高铁",
                    "transportFee": "200.00",
                    "days": 3.0,
                    "missionAllowance": "150.00",
                    "hotelExpense": "500.00",
                    "urbanTransport": "80.00",
                    "otherExpense": "0.00",

                },
                {
                    "uid": "a0252abe-a8ee-4730-bc56-0af1e081685a",
                    "startDate": "2020-08-01",
                    "startPoint": "常州",
                    "arriveDate": "2020-08-01",
                    "arrivePoint": "衡水",
                    "transport": "高铁",
                    "transportFee": "200.00",
                    "days": 3.0,
                    "missionAllowance": "150.00",
                    "hotelExpense": "500.00",
                    "urbanTransport": "80.00",
                    "otherExpense": "0.00",

                }
            ]
        }

        tpl.render(context)
        tpl.save('../PDF/listv2.docx')
        filename="listv2.docx"
        self.fhLibreOfficeFile(filename)
    def fhLibreOfficeFile(self,filename):
        status = 1
        inputdir = self.dirParam + "/PDF"
        outdir = self.dirParam + "/PDF"
        cmdParam ="/Applications/LibreOffice.app/Contents/MacOS/soffice"
        cmd = cmdParam + " --invisible  --convert-to pdf:writer_pdf_Export --outdir " + outdir + "  " + inputdir + "/" + filename
        print("cmd:", cmd)
        status = os.system(cmd)
        if (status == 0):
            print("status:", status)
        return status
if __name__ == "__main__":
    LibreOffic = LibreOfficeFile()
    LibreOffic.docToPdf()